<template>
  <div class="login-srceen">
    <Page-title title="资产管理数据驾驶舱" :full="false" :info-show="false" />
    <div class="login-container">
      <el-form
        ref="loginRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" maxlength="20" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" maxlength="20" show-password clearable />
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input v-model="loginForm.captcha" maxlength="4" clearable>
            <template #append>
              <img :src="codeImg" alt="验证码" @click="changeCode" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%; margin-top: 20px"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { postLogin } from "@/utils/srceen";
export default {
  name: "srceen-login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
        ],
        captcha: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "长度为 4 个字符", trigger: "blur" },
        ],
      },
      codeImg: "", // 验证码图片
      loading: false, // 登录按钮加载状态
    };
  },
  mounted() {
    let token = localStorage.getItem("token");
    if (token) {
      this.$message.success("您已登录");
      setTimeout(() => {
        this.$router.replace({ path: "/" });
      }, 2000);
    }
    this.changeCode();
  },
  methods: {
    // 登录
    handleLogin() {
      this.$refs.loginRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          postLogin(this.loginForm).then((res) => {
            if (res.code == 1) {
              localStorage.setItem("token", res.data.userinfo.token);
              localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));
              this.$router.push({ path: "/" });
            } else {
              this.changeCode();
              this.loginForm.captcha = "";
            }
          }).finally(() => {
            this.loading = false;
          })
        }
      });
    },
    // 切换验证码
    changeCode() {
      this.codeImg = `${
        process.env.VUE_APP_BASE_API
      }/index.php?s=/captcha&t=${new Date().getTime()}`;
    },
  },
};
</script>
<style lang="scss" scoped>
.login-srceen {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: url("~@/assets/srceen-bg.png") no-repeat;
  background-size: 100% 100%;
  .login-container {
    width: 400px;
    border-radius: 8px;
    background-color: rgba(11, 26, 87, 0.5);
    box-shadow: 0 0 10px #2753a2;
    backdrop-filter: blur(10px);
    padding: 20px 40px;
    margin: auto;
  }
}
::v-deep .el-form-item__label {
  color: #fff;
}
::v-deep .el-input__inner {
  background-color: rgba(11, 26, 87, 0.5);
  border: 1px solid #2753a2;
  color: #fff;
}
::v-deep .el-input-group__append {
  padding: 0;
  img {
    width: 100px;
    height: 100%;
    cursor: pointer;
  }
}
</style>
